import wx
import  wx.lib.editor    as  editor
import ctypes
import threading
import subprocess
import time
import re
from numpy.f2py.auxfuncs import getmultilineblock


def ForceBetween(self,min, val, max):
    if val  > max:
        return max
    if val < min:
        return min
    return val


def LineTrimmer(lineOfText):
    if len(lineOfText) == 0:
        return ""
    elif lineOfText[-1] == '\r':
        return lineOfText[:-1]
    else:
        return lineOfText

def LineSplitter(text):
    return map (LineTrimmer, text.split('\n'))   

class CustomEditor(editor.Editor):    
    
    def __init__(self,parent):
        
        colors = []
        self.colors = []
        self.colors.append(colors)
        editor.Editor.__init__(self,parent, -1, style=wx.SUNKEN_BORDER) 
        self.DefaultStyle = ['BLUE',self.bgColor,self.selectColor]
   
    def UpdateView(self, dc = None):
        print'c',self.cy
        print'updatelines',self.lines
        print'updatecolors',self.colors 
        if self.cy >= 2:
            self.DefaultStyle = None
        else:
            self.DefaultStyle = ['BLUE',self.bgColor,self.selectColor]
        if dc is None:
            dc = wx.ClientDC(self)
        if dc.Ok():
            self.SetCharDimensions()
            self.KeepCursorOnScreen()
            self.DrawSimpleCursor(0,0, dc, True)
            self.Draw(dc)
            
    def DrawLine(self, line, dc):
        if self.IsLine(line):
            l   = line
            t   = self.lines[l]
            cs   = self.colors[l]
            x = 0
            fragments = editor.selection.Selection(
                self.SelectBegin, self.SelectEnd,
                self.sx, self.sw, line, t)
            for (data, selected) in fragments:
                for i,ch in enumerate(data):  
                    c = cs[i] 
                    dc.SetTextForeground(c[0])
                    
                    if selected:
                        dc.SetTextBackground(c[2])
                        if x == 0 and len(ch) == 0 and len(fragments) == 1:
                            ch = ' '
                    else:
                        dc.SetTextBackground(c[1])
                    self.DrawEditText(ch, x, line - self.sy, dc)
                    x += len(ch)
    
    def SingleLineInsert(self, newText):
        ModLine = self.GetTextLine(self.cy)
        ModLine = ModLine[:self.cx] + newText + ModLine[self.cx:]
        self.SetTextLine(self.cy, ModLine)
        self.SingleColorInsert(newText)
        self.cHoriz(len(newText))
        self.TouchBuffer()
        self.UpdateView()
    
    def SingleColorInsert(self, newText):
        ModLine = self.GetColorsLine(self.cy)
        temp = []
        for i in range(len(newText)):
            temp.append([self.fgColor,self.bgColor,self.selectColor]) 
        ModLine = ModLine[:self.cx] + temp + ModLine[self.cx:]
        self.SetColorLine(self.cy, ModLine)
 
    def GetLength(self):
        length = 0
        for line in self.lines:
            length += len(line)
        return length   

    def InsertChar(self,char):
        if self.IsLine(self.cy):
            t = self.lines[self.cy]
            t = t[:self.cx] + char + t[self.cx:]
            self.SetTextLine(self.cy, t)
            self.InsertColor(self.cy)
            self.cHoriz(1)
            self.TouchBuffer()
            
    def SingleLineDelete(self, Row, bCol, eCol):
        ModLine = self.GetTextLine(Row)
        ModLine = ModLine[:bCol] + ModLine[eCol:]
        self.SetTextLine(Row,ModLine)
        self.SingleColorDelete(Row, bCol, eCol)
    
    def SingleColorDelete(self, Row, bCol, eCol):
        ModLine = self.GetColorsLine(Row)
        ModLine = ModLine[:bCol] + ModLine[eCol:]
        self.SetColorLine(Row,ModLine)
            
    def MultipleLineDelete(self, bRow, bCol, eRow, eCol):
        bLine = self.GetTextLine(bRow)
        eLine = self.GetTextLine(eRow)
        ModLine = bLine[:bCol] + eLine[eCol:]
        self.lines[bRow:eRow + 1] = [ModLine]
        self.MultipleColorsDelete(bRow, bCol, eRow, eCol)
    
    def MultipleColorsDelete(self, bRow, bCol, eRow, eCol):
        bLine = self.GetColorsLine(bRow)
        eLine = self.GetColorsLine(eRow)
        ModLine = bLine[:bCol] + eLine[eCol:]
        self.colors[bRow:eRow + 1] = [ModLine]
    

        
    def SetTextLine(self, lineNum, text):
        if self.IsLine(lineNum):
            self.lines[lineNum] = text
    
    def SetColorLine(self, lineNum, color):
        if self.IsLineColor:
            self.colors[lineNum] = color
            
    def InsertColor(self,lineNum):
        insertColors = [self.fgColor,self.bgColor,self.selectColor]
        if self.DefaultStyle != None:
            insertColors = self.DefaultStyle
        if self.IsLine(self.cy):
            c = self.colors[self.cy]
            c.insert(self.cx,insertColors)
            self.SetColorLine(self.cy, c)
        
    def DeleteChar(self,x,y,oldtext):
        newtext = oldtext[:x] + oldtext[x+1:]
        self.SetTextLine(y, newtext)
        self.DeleteColor(x,y,self.colors[y])
        self.TouchBuffer()
    
    def DeleteColor(self,x,y,oldcolor):
        if x<len(oldcolor):
            oldcolor.pop(x) 
        self.SetColorLine(y, oldcolor)

    def JoinLines(self):
        t1 = self.lines[self.cy]
        t2 = self.lines[self.cy+1]
        self.cx = len(t1)
        self.lines = self.lines[:self.cy] + [t1 + t2] + self.lines[self.cy+2:]
        self.JoinColors()
        self.TouchBuffer()
    
    def JoinColors(self):
        c1 = self.colors[self.cy]
        c2 = self.colors[self.cy+1]
        self.colors = self.colors[:self.cy] + [c1 + c2] + self.colors[self.cy+2:]
        
    def BreakLine(self, event):
        if self.IsLine(self.cy):
            t = self.lines[self.cy]
            self.lines = self.lines[:self.cy] + [t[:self.cx],t[self.cx:]] + self.lines[self.cy+1:]
            self.BreakLineColors()
            self.cVert(1)
            self.cx = 0
            self.TouchBuffer()
    
    def BreakLineColors(self):
        if self.IsLineColor(self.cy):
            t = self.colors[self.cy]
            self.colors = self.colors[:self.cy] + [t[:self.cx],t[self.cx:]] + self.colors[self.cy+1:]
    
    def IsLineColor(self, lineNum):
        return (0<=lineNum) and (lineNum<len(self.colors))
    

    def GetColorsLine(self, lineNum):
        if self.IsLineColor(lineNum):            
            return self.colors[lineNum]
        return []
        
  
    def SetInsertionPoint(self,col,row):
        if row > self.LinesInFile() - 1:
            self.MoveEndOfFile(None)
            for i in range(row - (self.LinesInFile() - 1)):
                self.BreakLine(None)
        self.cy = row
        lineleng = self.CurrentLineLength() 
        if lineleng < col:
            numspace = ' ' * (col - lineleng)
            self.cx = lineleng
            self.SingleLineInsert(numspace)
        self.cx = col
        self.UpdateView()

    
    def GetLastPosition(self):
        cy = len(self.lines) - 1
        cx = len(self.lines[cy])
        return (cx,cy)
    
    def GetInsertionPoint(self):
        return(self.cx,self.cy)
    

    def GetNumberOfLines(self):
        return self.LinesInFile()
    
    def GetLineLength(self,row):
        return len(self.lines[row])
                
    def GetTextRange(self, _from, _to):
        text = ''
        for line in self.lines:
            text+= line
            if len(text>=_to):
                text = text[_from:]
                return text[:-(_to-_from)]
        return ""    

    def GetLineText(self,row):   
        self.GetTextLine(self, row)
    
    
    
    def printcolors(self):
        for colors in self.colors:
            pass
            #printcolors
    
    def MultipleLinePaste(self, pastedLines):
        print 'MultipleLinePaste',pastedLines
        FirstLine = LastLine = self.GetTextLine(self.cy)
        FirstLine = FirstLine[:self.cx] + pastedLines[0]
        LastLine = pastedLines[-1] + LastLine[self.cx:]

        NewSlice = [FirstLine]
        NewSlice += [l for l in pastedLines[1:-1]]
        NewSlice += [LastLine]
        self.lines[self.cy:self.cy + 1] = NewSlice
        self.MultipleColorPaste(pastedLines)
        self.cy = self.cy + len(pastedLines)-1
        self.cx = len(pastedLines[-1])
        self.TouchBuffer()
        self.UpdateView()
        
    def MultipleColorPaste(self, pastedLines):
        print 'MultipleColorPaste',pastedLines
        FirstColor = LastColor = self.GetColorsLine(self.cy)
        FirstColor = FirstColor[:self.cx]
        for i in range(len(pastedLines[0])):
            FirstColor.append([self.fgColor,self.bgColor,self.selectColor])
        temp = []
        for i in range(len(pastedLines[-1])):
            temp.append([self.fgColor,self.bgColor,self.selectColor])  
        #printLastColor
        LastColor = temp + LastColor[self.cx:]
        #printLastColor

        NewSlice = []
        NewSlice.append(FirstColor)        
        for line in pastedLines[1:-1]:
            temp = []
            for i in range(len(line)):
                temp.append([self.fgColor,self.bgColor,self.selectColor])    
            NewSlice.append(temp)
        NewSlice.append(LastColor)
        self.colors = self.colors[:self.cy] + NewSlice + self.colors[self.cy+1:]
        
    def GetText(self, bRow, bCol, eRow, eCol):
        if bRow == eRow:
            return self.GetSingleLine(bRow, bCol, eCol)
        else:
            return self.GetMultipleLine(bRow, bCol, eRow, eCol)
        
    def GetSingleLine(self, Row, bCol, eCol):
        Line = self.GetTextLine(Row)
        return Line[bCol:eCol]

    def GetMultipleLine(self, bRow, bCol, eRow, eCol):
        bLine = self.GetTextLine(bRow)[bCol:]
        eLine = self.GetTextLine(eRow)[:eCol]
        return [bLine] + [l for l in self.lines[bRow + 1:eRow]] + [eLine]

    def MultipleLineReplace(self, bRow, bCol, eRow, eCol, strings):
        bLine = self.GetTextLine(bRow)
        eLine = self.GetTextLine(eRow)
        textadd = []
        for i in range(len(strings)):
            textadd.append(strings[i])
        textadd[0] = bLine[:bCol] + textadd[0]        
        textadd[len(textadd) - 1] = textadd[len(textadd) - 1] +  eLine[eCol+1:]
        self.lines[bRow:eRow + 1] = textadd   
        self.MultipleColorsReplace(bRow, bCol, eRow, eCol,strings)
        
    def MultipleColorsReplace(self, bRow, bCol, eRow, eCol,strings):
        sColor = self.GetColorsLine(bRow)
        eColor = self.GetColorsLine(eRow)
        coloradd = []
        for ele in strings:
            colors = []
            for k in range(len(ele)):
                colors.append([self.fgColor,self.bgColor,self.selectColor]) 
            coloradd.append(colors)
        coloradd[0] = sColor[:bCol] + coloradd[0]
        coloradd[len(coloradd) - 1] = coloradd[len(coloradd) - 1] +  eColor[eCol+1:]
        self.colors = self.colors[:bRow]+ coloradd + self.colors[eRow+1:] 
        
    def SimpleLineReplace(self, bRow, bCol, eCol, replaceLines):
        bLine = self.GetTextLine(bRow)
        textadd = []
        for i in range(len(replaceLines)):
            textadd.append(replaceLines[i])
        textadd[0] = bLine[:bCol] + textadd[0]        
        textadd[len(textadd) - 1] = textadd[len(textadd) - 1] +  bLine[eCol+1:]
        self.lines[bRow:bRow + 1] = textadd   
        self.SimpleColorsReplace(bRow, bCol, eCol,replaceLines)
        
    def SimpleColorsReplace(self, bRow, bCol, eCol,replaceLines):
        bColor = self.GetColorsLine(bRow)
        coloradd = []
        for ele in replaceLines:
            colors = []
            for k in range(len(ele)):
                colors.append([self.fgColor,self.bgColor,self.selectColor]) 
            coloradd.append(colors)
        coloradd[0] = bColor[:bCol] + coloradd[0]
        coloradd[len(coloradd) - 1] = coloradd[len(coloradd) - 1] +  bColor[eCol+1:]
        self.colors = self.colors[:bRow]+ coloradd + self.colors[bColor+1:] 
        
    def Replace(self,bRow,bCol,eRow,eCol,replaceLines):
        self.cy = bRow
        self.cx = bCol
        replaceLines = LineSplitter(replaceLines)
        if len(replaceLines) == 0:
            wx.Bell()
            return
        
        if bRow == eRow:
            self.SimpleLineReplace(bRow, bCol, eCol, replaceLines)
        else:
            self.MultipleLineReplace(bRow, bCol, eRow, eCol, replaceLines) 
    
        self.TouchBuffer()
        self.UpdateView()
    
    def Remove(self,bRow,bCol,eRow,eCol):
        if bRow == eRow:
            self.SingleLineDelete(bRow, bCol, eCol)
        else:
            self.MultipleLineDelete(bRow, bCol, eRow, eCol)
        self.TouchBuffer()
        self.cy = bRow
        self.cx = bCol
        self.UpdateView()
    
#    def RemoveColor(self,sRow,sCol,eRow,eCol):
#        sLine = self.GetColorsLine(sRow)
#        eLine = self.GetColorsLine(eRow)
#        print ' sLine[:sCol]', sLine[:sCol]
#        print 'eLine[eCol+1:]',eLine[eCol+1:]
#        restOfColors = self.colors[eRow + 1:]
#        self.colors = self.colors[:sRow] 
#        print 'restOfColors',self.colors 
#        self.colors.append(sLine[:sCol])
#        self.colors.append(eLine[eCol+1:])
#        restOfColors.extend(restOfColors)
#        print 'restOfColors',restOfColors 
#        print 'ModLine',self.colors

    def RemoveColor(self,sRow,sCol,eRow,eCol):
        sLine = self.GetColorsLine(sRow)
        eLine = self.GetColorsLine(eRow)
        ModLine = sLine[:sCol] + eLine[eCol+1:]
        print ' sLine[:sCol]', sLine[:sCol]
        print 'eLine[eCol+1:]',eLine[eCol+1:]
        print 'ModLine',ModLine
        self.colors[sRow:eRow+1] = ModLine 
        print 'ModLine',self.colors
        
    def WriteText(self,string):
        Lines = LineSplitter(string)
        if len(Lines) == 0:
            return
        elif len(Lines) == 1:
            self.SingleLineInsert(Lines[0])
        else:
            self.MultipleLinePaste(Lines)
        
class TextFrame(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Text Entry Example', size=(300, 250))
        panel = wx.Panel(self, -1, style=0)
        box = wx.BoxSizer(wx.VERTICAL)
        panel.SetSizer(box)
        panel.SetAutoLayout(True)
        
        self.text = CustomEditor(panel)
        #multiText = wx.TextCtrl(panel, -1,"test",size=(200, 100), style=wx.TE_MULTILINE)
        #multiText = stc.StyledTextCtrl(panel, 1000)
#        multiText = CustomEditor(parent = panel)
        #multiText = rt.RichTextCtrl(panel, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER|wx.WANTS_CHARS);
        '''
        multiText.AddText("Bui trinh minh tuan\n")
        multiText.AddText("Bui trinh minh tuan\n")
        multiText.AddText("Bui trinh minh tuan\n")
        multiText.AddText("Bui trinh minh tuan\n")
        multiText.AddText("Bui trinh minh tuan\n")
        multiText.AddText("Bui trinh minh tuan\n")
        multiText.AddText("Bui trinh minh tuan\n")
        multiText.AddText("Bui trinh minh tuan\n")
        '''
        #multiText = editor.Editor(panel, -1, style=wx.SUNKEN_BORDER)
#        multiText = rt.RichTextCtrl(panel, style=wx.VSCROLL|wx.HSCROLL)
        #multiText.SetInsertionPoint(0)
        box.Add(self.text, 1, wx.EXPAND)
#        sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
#        sizer.AddMany([multiLabel, multiText])
#        panel.SetSizer(sizer)
        self.text.Bind(wx.EVT_KEY_DOWN, self.onBack, self.text)
#        multiText.Bind(wx.EVT_, self.onBack,multiText)
#        multiText.WriteText("This is my second item.\n")
#        multiText.WriteText("This is my second item.\n")
#        multiText.WriteText("This is my second item.\n")
#        multiText.WriteText("This is my second item.\n")
#        multiText.WriteText("This is my second item.\n")
#        
#        multiText.WriteText("This is my second item.\n")
#        multiText.WriteText("This is my second item.\n")
#        multiText.WriteText("This is my second item.\n")
#        multiText.WriteText("This is my second item.\n")
       
#        multiText.SetText(["",
#                "This is a simple text editor, the class name is",
#                "Editor.  Type a few lines and try it out.",
#                "",
#                "It uses Windows-style key commands that can be overridden by subclassing.",
#                "Mouse select works. Here are the key commands:",
#                "",
#                "Cursor movement:     Arrow keys or mouse",
#                "Beginning of line:   Home",
#                "End of line:         End",
#                "Beginning of buffer: Control-Home",
#                "End of the buffer:   Control-End",
#                "Select text:         Hold down Shift while moving the cursor",
#                "Copy:                Control-Insert, Control-C",
#                "Cut:                 Shift-Delete,   Control-X",
#                "Paste:               Shift-Insert,   Control-V",
#                ""])
#        event = ''
#       
#        multiText.MoveEndOfFile(event)
#        multiText.BreakLine(event)
#        multiText.BreakLine(event)
#        multiText.BreakLine(event)
#        multiText.BreakLine(event)
#        multiText.BreakLine(event)
#        multiText.SingleLineInsert('buitrinhminhtuan')
#        #printmultiText.LinesInFile()
        #multiText.SetText([''])
        self.flag = True
        ##printlist(multiText.GetText())
#        self.text = multiText
        
        self.x = 0
        self.y = 0
        #self.text.SetFont(wx.Font(self.text.GetFont().GetPointSize() + 3, wx.ROMAN, wx.FONTWEIGHT_NORMAL, wx.BOLD, True))
        #self.text.SetCharDimensions()
    def _OnProcessError(self,event):
        #print'yahoo'
        pass
    
    def SetInsertpoint(self, col, row):
        if row > self.text.LinesInFile() - 1:
            self.text.MoveEndOfFile(None)
            for i in range(row - (self.text.LinesInFile() - 1)):
                self.text.BreakLine(None)
        self.text.cy = row
        lineleng = self.text.CurrentLineLength()
        if lineleng < col:
            numspace = ' ' * (col - lineleng)
            self.text.cx = lineleng
            self.text.SingleLineInsert(numspace)
        self.text.cx = col
        self.text.UpdateView()
        #self.text.Draw(None)
        
    def SetInsertpoint2(self, col, row):
        try:
            if row > self.text.GetNumberOfLines() - 1:
                insertline = '\r\n' * (row - (self.text.GetNumberOfLines() - 1))
                self.text.AppendText(insertline)
            
            lineleng = self.text.GetLineLength(row)
            if lineleng < col:
                numspace = ' ' * (col - lineleng)
                self.text.SetInsertionPoint(self.text.XYToPosition(lineleng, row))
                self.text.WriteText(numspace)
                
            #print'truoc loi'
            #print'col, row', col, row
            #printself.text.GetNumberOfLines()
            #printself.text.GetLineLength(row)
            self.text.SetInsertionPoint(self.text.XYToPosition(col, row))
            #print'sau loi'
        except Exception, e:
            #print'Loi Setinsertpoint'
            #printe
            wx.MessageBox('waiting....', 'Error!', wx.OK)
               
    def onBack(self, event):
        keycode = event.GetKeyCode()
        #print'da vao day'
        if keycode == wx.WXK_HOME:
            pass
            #printlist(self.text.GetText())
#        elif keycode == wx.WXK_INSERT:
#            if self.flag:
#                self.text.SetSelection(0,29)
#                self.text.ReplaceSelection('minh tuan aaaaaaaa aaaaa aaaa')
#                #self.text.UpdateView()
##                self.x += 1
#                self.flag = False
#            else:
#
#                self.text.SetSelection(0,10)
#                self.text.ReplaceSelection('minh tuan bbbbbbb bbbbbb bbbb')
#                #self.text.UpdateView()
##                self.x += 1
#                self.flag = True
#            return

        elif keycode == wx.WXK_END:
            self.text.GotoLine(0)
            self.text.DelLineRight()
            if self.flag:
                self.text.AddText("Bui Trinh Minh Tuan")
            else :
                self.text.AddText("aaaaaaaaaaaaaaaaaaaaaa")
                
            self.text.GotoLine(self.text.GetLineCount() - 1)
            self.text.DelLineRight()
            if self.flag:
                self.text.AddText("aaaaaaaaaaaaaaaaaaaaaa")
            else :
                self.text.AddText("Bui Trinh Minh Tuan")
            if self.flag:
                self.flag = False;
            else:
                self.flag = True;
        elif keycode == wx.WXK_F1:
            print 'gettext',self.text.GetText(1,1,5,0)
            return
        
        elif keycode == wx.WXK_F2:
            self.text.Replace(1,1,5,0,'laun')
            return
        
        elif keycode == wx.WXK_DELETE: 
            #print'delete'
            self.text.Remove(1,0, 5, 0)
#            app1 = com3(self.text)
#            app1.start()
           
#            #self.text.AddText('\n')
#            indexend = self.text.LineLength(self.text.GetCurrentLine())
#            self.text.SetTargetStart(self.text.GetCurrentPos())
#            self.text.SetTargetEnd(self.text.GetCurrentPos()+ indexend - 1)
#            #printindexend,self.text.GetTargetStart(),self.text.GetTargetEnd()
#            self.text.ReplaceTarget('bbbbbbbbbbbbbb1')
           
            return
        elif keycode == wx.WXK_BACK:
            event.Skip()
            return
            #printself.text.GetColumn(self.text.GetCurrentPos())
            #printself.text.PositionFromLine(self.text.GetCurrentLine())
            #printself.text.GetCurrentPos()
            self.text.SetSelection(0,self.text.GetTextLength() - 6)
            self.text.ReplaceSelection(' ')
            self.text.GotoPos(self.text.GetTextLength()+10)
#            self.text.SetSelection(0, self.text.PositionFromLine(4))
#            self.text.ReplaceSelection('')# 
#            self.text.GotoPos(self.text.FindColumn(2,3))
            
#            indexend = self.text.LineLength(self.text.GetCurrentLine())
#            self.text.SetTargetStart(self.text.GetCurrentPos())
#            self.text.SetTargetEnd(self.text.GetCurrentPos()+ indexend - 1)
#            #printindexend,self.text.GetTargetStart(),self.text.GetTargetEnd()
#            self.text.ReplaceTarget('bbbbbbbbbbbbbb1')
#            self.text.DelLineLeft()
            return
        
        elif keycode == wx.WXK_INSERT:
            #printself.text.GetLastPosition()
            #printself.text.GetInsertionPoint()
            self.text.WriteText('Bui Trinh Minh Tuan\naaaaaaaaaaaaaaaaaa\ndddddddddddddddd\n')
            return
        elif keycode == wx.WXK_F1:
#            self.text.Replace(2, 0, 3, 0, 'test replace text')
            self.text.Remove(2, 0, 2, 4) 
        elif keycode == wx.WXK_F2:
            self.text.SetInsertionPoint(10, 10)
        elif keycode == wx.WXK_F3:
            self.text.SetInsertionPoint(0, 8)
        elif keycode == wx.WXK_F4:
            self.text.SetInsertionPoint(5, 0)
        elif keycode == wx.WXK_F5:
            self.text.SetInsertionPoint(2, 50)
            
        event.Skip()
        #self.text.OnClick()
class com2(threading.Thread):
    def __init__(self,text):
        super(com2, self).__init__()
        self.text = text
        self.flag = True
        
    def run(self):
        
        for i in range(1000):
            
            self.text.AddText('bui trinh minh tuan\naaaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbb\n')
            self.text.GotoPos(self.text.GetTextLength())

class com3(threading.Thread):
    def __init__(self,text):
        threading.Thread.__init__(self) 
        self.text = text
        self.flag = True
        
    def run(self):
        time.sleep(0.1)
        self.text.SelectOff()
        for i in range(1):
            if self.flag:
                self.text.SetTextLine(1,'Bui trinh minh tuan')
                self.flag = False
            else:
                self.text.SetTextLine(1,'aaaaaa aaaaaa aaaaa')
                self.flag = True
            listColors =self.text.GetColorsLine(1)
            for colors in listColors:
                pass
                #print'color:',colors
                
#        self.text.UpdateView() 
        #self.text.AdjustScrollbars()
            
                                
class com1(threading.Thread):
    def __init__(self,text):
        threading.Thread.__init__(self) 
        self.text = text
        self.flag = True
        
    def run(self):
        for i in range(5):
            #self.text.Remove(0,self.text.GetLineLength(0))
            #printself.text.GetLineLength(0)
            if self.flag:
                self.text.Replace(3,self.text.GetLineLength(0),'Bui trinh minh tuan')
                self.flag = False
            else:
                self.text.Replace(0,self.text.GetLineLength(0),'aaaaaa aaaaaa aaaaa')
                self.flag = True
            #self.text.AddText('bui trinh minh tuan\naaaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbb\n')
            #self.text.GotoPos(self.text.GetTextLength())
       
        
app = wx.PySimpleApp()
frame = TextFrame()
frame.Show()
app.MainLoop()
